<template>
    <div id="AccountManage">
    	<el-button 
    		type="primary" 
    		style="margin-bottom:20px;" 
    		@click="showAddNew=true,userInfo={roles:[]},isAddNew=true"
    	>
    		新增账号
    	</el-button>
        <table>
            <thead>
                <tr>
                    <th>账号名称</th>
                    <th>角色列表</th>
                    <th>操作</th>
                </tr>
            </thead>
            <tbody>
                <tr v-for="(item,key) in allUsers" :key="key">
                    <td>{{item.username}}</td>
                    <td>
                        <span v-for="(ele,index) in item.roles" :key="index"
                            v-accountRole="allRoles"
                            style="margin-right:20px"
                        >
                            {{ele}}
                        </span>
                    </td>
                    <td>
                        <el-button 
                        	type="primary" 
                        	size="mini" 
                        	@click="showAddNew=true,userInfo=item,isAddNew=false"
                        >
                        	编辑
                        </el-button>
                        <el-button 
                        	type="danger" 
                        	size="mini" 
                        	@click="deleteUser(item)"
                        >
                        	删除
                        </el-button>
                    </td>
                </tr>
            </tbody>
        </table>
        <add-new-account 
        	v-if="showAddNew" 
        	:userInfo="userInfo" 
        	:isAddNew="isAddNew"
        >
    	
        </add-new-account>
    </div>
</template>
<script>
import addNewAccount from './AddNewAccount'
export default {
    data(){
        return {
            showAddNew: false,
            userInfo:{roles:[]}
        }
    }, 
    computed: {
        allUsers(){
            return this.$store.getters.allUsers;
        },
        allRoles(){
            return this.$store.getters.allRoles;
        }
    },
    methods: {
        deleteUser(item){
            if(item.username=="zmt"){
                this.$alert('无法删除该账号', '警告', {
                confirmButtonText: '确定',
                });
            }else{
                this.$http.post(this.$api.deleteUser,{_id:item._id})
                .then((res)=>{
                    this.$message({
                    message: res.message,
                    type: res.success?'success':'danger'
                    });
                    this.$store.dispatch('loadAllUsers')        
                })
            }
        }
    },
    components: {addNewAccount},
    created() {
        this.$store.dispatch('loadAllUsers')
    },
}
</script>

<style lang="scss" scoped>

</style>
